package com.hospital.KT_Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.hospital.BaseActivity.BaseActivity;
import com.hospital.activity.R;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by wangjun on 2015/7/3.
 */
public class IndexActivity extends BaseActivity {

    private ViewPager viewPager = null;
    private List<ImageView> imageViews = null;

    private int[] imageResId; // 图片ID
    private List<View> dots; // 图片标题正文的那些点
    private int currentItem = 0; // 当前图片的索引号
    private ScheduledExecutorService scheduledExecutorService;//定时执行任务

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            viewPager.setCurrentItem(currentItem);//切换当前显示的图片
        }
    };

    private LinearLayout doctor_ll = null;
    private LinearLayout doctor_l2 = null;
    private LinearLayout user_center = null;


    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.index);
        mContext=this;
        // mPageName="导航首页";
        //  init();
        //图片资源
        this.viewPager = (ViewPager) super.findViewById(R.id.vPager);
        imageResId = new int[]{R.drawable.sy14, R.drawable.sy14,
                R.drawable.sy14, R.drawable.sy14, R.drawable.sy14};

        //获取按钮控件
        doctor_ll = (LinearLayout)findViewById(R.id.doctor_ll);//家庭医生
        doctor_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(IndexActivity.this,DoctorListActivity.class);
                startActivity(i);
            }
        });
        doctor_l2 = (LinearLayout)findViewById(R.id.doctor_l2);//预约诊疗
        doctor_l2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(IndexActivity.this,DoctorQueryActivity.class);
                startActivity(i);
            }
        });
        user_center = (LinearLayout)findViewById(R.id.usercenter);//用户中心
        user_center.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(IndexActivity.this,UserCenterActivity.class);
                startActivity(i);
            }
        });

        //初始化图片
        imageViews = new ArrayList<ImageView>();
        for (int i = 0; i < imageResId.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(imageResId[i]);
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            imageViews.add(imageView);
        }
        //定义小圆点
        dots = new ArrayList<View>();
        dots.add(findViewById(R.id.v_dot0));
        dots.add(findViewById(R.id.v_dot1));
        dots.add(findViewById(R.id.v_dot2));
        dots.add(findViewById(R.id.v_dot3));
        dots.add(findViewById(R.id.v_dot4));
        viewPager = (ViewPager) findViewById(R.id.vPager);//获取适配器控件vp
        viewPager.setAdapter(new MyPictureAdapter());//设置填充ViewPager页面的适配器
        //设置一个监听器，当ViewPager中的页面改变时调用
        viewPager.setOnPageChangeListener(new MyPageChangeListener());
    }
    /**
     * 换行切换任务
     */

    private class ScrollTask implements Runnable {

        public void run() {
            synchronized (viewPager) {
                //viewPager.setAnimation(out);
                System.out.println("currentItem: " + currentItem);
                currentItem = (currentItem + 1) % imageViews.size();//每运行一次图片切换，更改图片索引号
                // viewPager.setAnimation(in);
                handler.obtainMessage().sendToTarget(); // Handler切换图片
            }
        }
    }
    @Override
    protected void onStart() {
        scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        //当Activity显示出来后，每三秒钟切换一次图片显示
        scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 2, 4, TimeUnit.SECONDS);
        super.onStart();
    }
    @Override
    protected void onStop() {
        // 当Activity不可见的时候停止切换
        scheduledExecutorService.shutdown();
        super.onStop();
    }
    /**

     * * 当ViewPager中页面的状态发生改变时调用
     * *
     */
    private class MyPageChangeListener implements ViewPager.OnPageChangeListener {
        private int oldPosition = 0;

        /**
         * This method will be invoked when a new page becomes selected.
         * position: Position index of the new selected page.
         */
        public void onPageSelected(int position) {
            dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);//加载小圆点的正常背景图
            dots.get(position).setBackgroundResource(R.drawable.dot_focused);//更改背景图为选中
            oldPosition = position;//当前position成为旧的position
        }
        public void onPageScrollStateChanged(int arg0) {

        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }
    }
    /**
     * 填充ViewPager页面的适配器
     * *
     * * @author Administrator
     */
    private class MyPictureAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return imageResId.length;
        }//获取图片总数

        @Override
        public void startUpdate(View view) {

        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(imageViews.get(arg1));
            return imageViews.get(arg1);
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);
        }

        @Override
        public void finishUpdate(View view) {
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

        @Override
        public Parcelable saveState() {
            return null;
        }
    }

}


